﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookBuy
{
    public partial class TimKiemNangCao :BasePage
    {
        muasachEntities entities = new muasachEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<THELOAI> lsDanhMuc = entities.THELOAIs.ToList();
                ddlTheLoai.Items.Add(new ListItem("Tất Cả", "-1", true));
                foreach (THELOAI dm in lsDanhMuc)
                {
                    ddlTheLoai.Items.Add(new ListItem(dm.TENTHELOAI, dm.MATHELOAI.ToString()));
                }
            }
            else
            {
                TimKiem();
            }
                

        }

        public List<SANPHAM> dsKetQua = new List<SANPHAM>();
        protected void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void TimKiem()
        {
            string tenSach = txtTenSach.Text;
            int maTheLoai = int.Parse(ddlTheLoai.SelectedValue);
            int giaTu = int.Parse(ddlGiaTu.SelectedValue);
            int giaDen = int.Parse(ddlGiaDen.SelectedValue);
            string tacGia = txtTacGia.Text;
            dsKetQua = entities.SANPHAMs.Where(sp => sp.TENSP.Contains(tenSach)
                                && sp.TACGIA.Contains(tacGia)).ToList();
            if (maTheLoai != -1)
            {
                dsKetQua = dsKetQua.Where(sach => sach.MATHELOAI == maTheLoai).ToList();
            }

            dsKetQua = dsKetQua.Where(sach => int.Parse(sach.GIABAN) >= giaTu
                                        && int.Parse(sach.GIABAN) <= giaDen).ToList();

        }

    }
}